package com.iflytek.autofly.setting.biz.secret;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemProperties;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.iflytek.autofly.setting.R;
import com.iflytek.autofly.setting.biz.base.BaseFragment;
import com.iflytek.autofly.utils.FlyLog;

/**
 * <b>Description: </b>声音设置的fragment<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015年10月14日</td>
 * <td>上午11:34:14</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class AdvanceSettingFragment extends BaseFragment {
    private static final String PROPERTY_SYSUP_TEST = "persist.sys.update.test";
    private static final String PROPERTY_SYSUP_CHANNEL = "persist.sys.update.channel";
    /**
     * log等级设置view的id
     */
    final private int[] mLogLevelCheckBoxIds = {R.id.advance_log_level_wtf_cb,
            R.id.advance_log_level_error_cb, R.id.advance_log_level_warn_cb,
            R.id.advance_log_level_info_cb, R.id.advance_log_level_debug_cb,
            R.id.advance_log_level_verbose_cb,};
    /**
     * log等级
     */
    final private int[] mLogLevels = {FlyLog.LEVEL_WTF, FlyLog.LEVEL_ERROR,
            FlyLog.LEVEL_WARN, FlyLog.LEVEL_INFO, FlyLog.LEVEL_DEBUG,
            FlyLog.LEVEL_VERBOSE};
    /**
     * log输出设置view的id
     */
    final private int[] mLogOutputCheckBoxIds = {
            R.id.advance_log_output_thr_name_cb,
            R.id.advance_log_output_thr_id_cb,
            R.id.advance_log_output_class_cb,
            R.id.advance_log_output_method_cb, R.id.advance_log_output_file_cb,
            R.id.advance_log_output_stack_cb,};
    /**
     * log输出
     */
    final private int[] mLogOutputs = {FlyLog.OUTPUT_THREAD_NAME,
            FlyLog.OUTPUT_THREAD_ID, FlyLog.OUTPUT_CLASS, FlyLog.OUTPUT_METHOD,
            FlyLog.OUTPUT_FILE, FlyLog.OUTPUT_STACK};
    private View mView;
    /**
     * log等级设置的view
     */
    private CheckBox[] mLogLevelCheckBoxes;
    /**
     * log输出设置view
     */
    private CheckBox[] mLogOutputCheckBoxes;
    private LogSetting mLogSetting;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mView = view;
        init(view);
        return view;
    }

    @Override
    public void renew() {

    }

    private void init(View v) {
        addUnit(R.layout.log, R.layout.log_sub);
        addUnit(R.layout.sysupconfig, R.layout.sysupconfig_sub);
        addUnit(R.layout.original_app, R.layout.original_app_sub);
//        addUnit(R.layout.reboot_recovery);

        initLog(v);
        initSysUp(v);
        initOriApp(v);
//        initRebootRecovery(v);
    }

    private void initLog(View v) {

        mLogSetting = LogSetting.getInstance();

        mLogLevelCheckBoxes = new CheckBox[mLogLevelCheckBoxIds.length];
        mLogOutputCheckBoxes = new CheckBox[mLogOutputCheckBoxIds.length];
        for (int i = 0; i < mLogLevelCheckBoxIds.length; i++) {
            final int index = i;
            CheckBox cb = (CheckBox) v.findViewById(mLogLevelCheckBoxIds[i]);
            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked) {
                    if (isChecked) {
                        mLogSetting.enableLevel(mLogLevels[index]);
                    } else {
                        mLogSetting.disableLevel(mLogLevels[index]);
                    }
                }
            });
            cb.setChecked(mLogSetting.isLevelEnabled(mLogLevels[index]));
            mLogLevelCheckBoxes[i] = cb;
        }

        for (int i = 0; i < mLogOutputCheckBoxIds.length; i++) {
            final int index = i;
            CheckBox cb = (CheckBox) v.findViewById(mLogOutputCheckBoxIds[i]);
            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked) {
                    if (isChecked) {
                        mLogSetting.enableOutput(mLogOutputs[index]);
                    } else {
                        mLogSetting.disableOutput(mLogOutputs[index]);
                    }
                }
            });
            cb.setChecked(mLogSetting.isOutputEnabled(mLogOutputs[index]));
            mLogOutputCheckBoxes[i] = cb;
        }

        /* assert设置view */
        ToggleButton assertTbn = (ToggleButton) v.findViewById(R.id.advance_log_output_stack_tbn);
        assertTbn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    mLogSetting.enableAssert();
                } else {
                    mLogSetting.disableAssert();
                }
            }
        });
        assertTbn.setChecked(mLogSetting.isAssertOn());
    }

    private void initSysUp(View v) {
        CheckBox isTestCb = (CheckBox) v.findViewById(R.id.advance_sysup_test_cb);
        isTestCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SystemProperties.set(PROPERTY_SYSUP_TEST, isChecked ? "1" : "0");
            }
        });
        isTestCb.setChecked(SystemProperties.getInt(PROPERTY_SYSUP_TEST, 0) != 0);

        final EditText channelEt = (EditText) v.findViewById(R.id.advance_sysup_channel_et);
        channelEt.setText(SystemProperties.get(PROPERTY_SYSUP_CHANNEL, "stable"));

        Button channelSetBtn = (Button) v.findViewById(R.id.advance_sysup_channel_set_btn);
        channelSetBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SystemProperties.set(PROPERTY_SYSUP_CHANNEL, channelEt.getText().toString());
            }
        });
    }

    private void initOriApp(View v) {

        TextView settingTv = (TextView) v.findViewById(R.id.advance_ori_app_setting);
        settingTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity("com.android.settings", "com.android.settings.Settings");
            }
        });
        TextView applistTv = (TextView) v.findViewById(R.id.advance_ori_app_applist);
        applistTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity("com.iflytek.autofly.applist", "com.iflytek.autofly.applist.AppListMainActivity");
            }
        });
    }

    private void initRebootRecovery(View v) {

        RelativeLayout rebootRecoveryRl = (RelativeLayout) v.findViewById(R.id.advance_reboot_recovery_rl);
        rebootRecoveryRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
                pm.reboot("recovery");
            }
        });

    }

    private void startActivity(String pn, String cn) {

        Intent intent = new Intent();
        intent.setClassName(pn, cn);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        getActivity().startActivity(intent);
    }

    @Override
    public void processCommandline(String cmd) {

    }

    @Override
    public String[] getCommands() {
        return new String[0];
    }


}
